package demo.database.mysql.demo.D02_性能测试;


import java.sql.Connection;
import java.sql.PreparedStatement;

import com.titan.core.utils.db.DBUtils;
import com.titan.core.utils.debug.Timer;
import com.titan.core.utils.io.ConsoleUtils;

import demo.database.mysql.demo.utils.MySqlUtils;

public class D02_写入性能测试 {
    private static String url = "jdbc:mysql://127.0.0.1:3306/s3101";
    private static String userid = "root";
    private static String password = "123456";
    
    private static void run() throws Exception {
        run(100000);   // [6分 11秒 860毫秒] [用时(ms)=371860]
    }
    private static void run(int count) throws Exception {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = MySqlUtils.getConnection(url, userid, password);
            
            DBUtils.execute(conn, "DROP TABLE IF EXISTS `cc`");
            DBUtils.execute(conn, "CREATE TABLE `cc` (`id` varchar(40) PRIMARY KEY, `data` int, `create_dt` datetime)");
            
            stmt = conn.prepareStatement("INSERT INTO cc VALUES (UUID(), ?, NOW())");
            for(int i = 0; i < count; i++){
                stmt.setInt(1, i);
                stmt.execute();
            }
            
            String value = DBUtils.getValue(conn, "select count(*) from cc");
            ConsoleUtils.log("value", value);

//            DBUtils.execute(conn, "DROP TABLE IF EXISTS `cc`");
        } finally {
            DBUtils.close(stmt);
            DBUtils.close(conn);
        }
    }    


    public static void main(String[] args) {
        Timer timer = Timer.$();
        try {
            run();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            timer.print();
        }
    }
}
